/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builders;

import java.util.ArrayList;
import java.util.List;
import org.balisunrise.annotation.util.AnnotationsUtilitiesException;
import org.balisunrise.annotation.util.CriteriaUtilities;
import org.balisunrise.annotation.util.FilterUtilities;
import org.balisunrise.annotation.util.NameUtil;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.EntityFilter;
import org.balisunrise.common.FieldLayout;
import org.balisunrise.common.FilterField;
import org.balisunrise.swing.components.panel.FieldsPanel;
import org.balisunrise.swing.components.panel.layouts.DefaultFieldLayoutBuilder;

/**
 *
 * @author glauco
 */
public class EntityFilterBuilder implements
        org.balisunrise.builder.EntityFilterBuilder{

    private static final String MESSAGE = "Erro ao construir uma EntityFilter.\n";

    @Override
    public EntityFilter create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        try{
            FilterUtilities fu = FilterUtilities.create(entityClass, alias);
            return create(fu);
        }catch(AnnotationsUtilitiesException aue){
            throw new BuilderException(MESSAGE +
                    aue.getMessage(), aue.getCause());
        }
    }

    @Override
    public EntityFilter create(FilterUtilities fu) throws BuilderException {

        if(fu == null)
            throw new BuilderException(MESSAGE +
                    "filterUtilities não pode ser nulo.");

        org.balisunrise.swing.commons.EntityFilter ef =
                new org.balisunrise.swing.commons.EntityFilter();

        ef.setAlias(fu.alias);
        ef.setEntityClass(fu.entityClass);
        ef.setTitle(fu.title);

        // lista dos campos
        List<FieldLayout> aux = new ArrayList<FieldLayout>();

        // cria os filtros do painel de filtros
        for (CriteriaUtilities cr : fu.filters) {
            FilterField ff = Application.get()
                    .getVisualEntityManager()
                    .getFilterFieldBuilder()
                    .buildFilterField(cr);
            ff.setOwner(ef);
            aux.add(ff);
            ef.getLayouts().add(ff);
        }

        // cria representação visual
        FieldsPanel visualRepresentation =
                new FieldsPanel(aux, new DefaultFieldLayoutBuilder());

        // atribui representação visual
        ef.setVisualRepresentation(visualRepresentation);

        // nome do painel
        String name = NameUtil.buildName(ef);
        visualRepresentation.setName(name);
        visualRepresentation.getAccessibleContext().setAccessibleName(name);
        ef.setName(name);

        return ef;
    }
}
